﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Dynamic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Mall.Core;
using Mall.Data;
using Solution;
using Webdiyer.WebControls.Mvc;
using Solution.Provide;

namespace Mall.AdminSystem.Controllers
{
    /// <summary>
    /// 用户管理
    /// </summary>
    [Authorize]
    public class ManagerController : BaseController
    {
        private AdminCore aCore = new AdminCore();
        private RoleCore rCore = new RoleCore();

        /// <summary>
        /// 用户管理
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Index(int id = 0)
        {

            //var cc = aCore.LoadEntities().ToList().Select(o =>
            //{
            //    dynamic rs = new ExpandoObject();
            //    rs.A_Account = o.A_Account;
            //    rs.A_Id = o.A_Id;
            //    rs.A_Sex = o.A_Sex;
            //    rs.A_Name = o.A_Name;
            //    rs.A_Time = o.A_Time;
            //    rs.A_Status = o.A_Status;
            //    rs.A_Phone = o.A_Phone;
            //    rs.A_IsAdmin = o.A_IsAdmin;
            //    return rs;
            //});

            //dynamic rs = new ExpandoObject();
            //ViewBag.Rs = aCore.LoadEntities().ToList().Select(o => new
            //{
            //    o.A_Account,
            //    o.A_Id,
            //    o.A_Sex,
            //    o.A_Name,
            //    o.A_Time,
            //    o.A_Status,
            //    o.A_Phone,
            //    o.A_IsAdmin
            //}).ToList().ToDynamic();

            //ViewBag.T1 = (new { name = "asd" }).ToDynamic();

            ViewBag.RoleList = rCore.LoadEntities(o => o.R_State == 0).OrderBy(o => o.R_Flag).ToList();

            var rs = aCore.LoadEntities().Include("Sys_AdminRole").OrderByDescending(o => o.A_Time).ToPagedList(id, 10);
            return View(rs);
        }


        /// <summary>
        /// 管理人员的个人信息
        /// </summary>
        /// <returns></returns>
        public ActionResult Info()
        {
            return View(new Sys_Admin());
        }


        /// <summary>
        /// 冻结/解冻
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult Forzen(Guid id)
        {
            StandardJsonResult rs = new StandardJsonResult();
            var info = aCore.LoadEntity(o => o.A_Id == id);

            if (info != null)
            {
                var state = info.A_Status;
                if (state == 0)
                {
                    state = 1;
                }
                else if (state == 1)
                {
                    state = 0;
                }
                info.A_Status = state;
                if (aCore.Update(info)) //更改状态
                {
                    rs.State = 0;
                }
            }
            return Json(rs,JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult ResetPwd(Guid id)
        {
            StandardJsonResult rs = new StandardJsonResult();
            var obj = aCore.LoadEntity(o => o.A_Id == id);

            string pwdCode = new RandomStringHelper().GetRandomString(4);//登录密码干扰码 
            obj.A_Pwd = CryptoService.Md5Encrypt("888888" + pwdCode);
            obj.A_EncryptCode = pwdCode;

            if (aCore.Update(obj)) //更改密码
            {
                rs.State = 0;
            }

            return Json(rs, JsonRequestBehavior.AllowGet);
        }
        [HttpPost]
        public JsonResult SaveRole(Guid id,string roleStr)
        {
            StandardJsonResult rs = new StandardJsonResult();
            if (aCore.SaveRole(id,roleStr))
            {
                rs.State = 0;
            }

            return Json(rs, JsonRequestBehavior.AllowGet);
        }


        /// <summary>
        /// 编辑管理员
        /// </summary>
        /// <returns></returns>
        public ActionResult Edit(Guid? id)
        {
            Sys_Admin obj = null;

            //TempData["RoleList"] = EnumHelper.GetEnumKeysSelectListItems<ManagerRoleEnum>();

            if (id.HasValue)
            {
                obj = aCore.LoadEntity(o => o.A_Id == id);
            }
            else
            {
                obj = new Sys_Admin();
            }
            return View(obj);
        }

        /// <summary>
        /// 保存管理员
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Edit(Sys_Admin obj)
        {
            obj.A_Time = DateTime.Now;
            if (obj.A_Id == Guid.Empty) //新增
            {
                obj.A_Id = Guid.NewGuid();
                aCore.AddEntity(obj);
            }
            else //修改
            {
                aCore.Update(obj, true, "A_Account", "A_Name", "A_Phone", "A_Sex", "A_Remark");
            }

            return RedirectToAction("Index");
        }


    }
}